package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(path = "/member")

public class MemberController {

    @Reference
    private MemberService memberService;

    @GetMapping(path = "/findPage")
    public Result findAll(QueryPageBean queryPageBean){
        try {
            PageResult pageResult = memberService.pageQuery(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    //根据id查询数据详情
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id){

        try {
            Member member = memberService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    //新增
    @PostMapping(path = "/add")
    public Result add(@RequestBody Member member){

        try {
            member.setRegTime(new Date());
            member.setStatus(1);
            memberService.add(member);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }


    //编辑
    @PutMapping(path = "/update")
    public Result edit(@RequestBody Member member){

        try {
            memberService.edit(member);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    //查询所有
    @GetMapping(path = "/findAll")
    public Result findAll(){

        try {
            List<Member> memberList = memberService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, memberList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //修改会员的状态
    @PutMapping("/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Integer id,
                               @PathVariable("status") Integer status){
        try {
            memberService.update(id,status);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

}

